package com.layui.ad.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.layui.ad.entity.BrokerInfo;
import com.layui.ad.entity.SysAdmin;
import com.layui.ad.enums.BrokerEnum;
import com.layui.ad.service.BrokerService;
import com.layui.ad.utils.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.beans.IntrospectionException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/broker")
public class BrokerController extends BaseController {

    @Autowired
    private BrokerService brokerService;

    @RequestMapping("index")
    public String index(HttpServletRequest request) {
        request.setAttribute("act", "index");
        request.setAttribute("con", "broker");
        request.setAttribute("brokerEnum", BrokerEnum.values());
        return "broker/index";
    }

    @ResponseBody
    @RequestMapping("getlist")
    public JsonResult getList(@RequestParam("page") Integer current, Integer limit, Integer usertitle, String keywords) {
//        Integer current = new Integer(request.getParameter("page"));
//        Integer size = new Integer(request.getParameter("limit"));
        Integer cityId = getCurrUser().getCityId();
        Page<BrokerInfo> page = new Page<>(current, limit);
        BrokerInfo binfo = new BrokerInfo();
        binfo.setCompanysid(cityId);
        if (null != usertitle) {
            binfo.setUsertitle(usertitle);
        }
        if (null != keywords) {
            binfo.setBname(keywords);
        }
        IPage<BrokerInfo> brokerInfoIPage = brokerService.selectRecord(page, binfo);
        return JsonResult.ok().set("data", brokerInfoIPage.getRecords()).set("count", brokerInfoIPage.getTotal());
    }

    @ResponseBody
    @RequestMapping("setStatus")
    public JsonResult setStatus(Integer brokerId, Integer status) {
        int num = brokerService.setBrokerStatus(brokerId, status);
        if (0 != num) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail("调整经纪人状态失败");
        }
    }

    @ResponseBody
    @RequestMapping("drop")
    public JsonResult drop(Integer id){
        try{
            brokerService.delBroker(id);
            return JsonResult.ok();
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }


    @RequestMapping("edit")
    public String edit(HttpServletRequest request,Integer id){
        request.setAttribute("act", "doedit");
        request.setAttribute("con", "broker");
        request.setAttribute("brokerList",BrokerEnum.values());
        BrokerInfo data = brokerService.getInfoByBrokerId(id);
        request.setAttribute("data",data);
        return "broker/edit";
    }

    @ResponseBody
    @RequestMapping("doedit")
    public JsonResult doedit(Integer id,Integer userTitle,Integer netBroker){
        Map brokerData = new HashMap<>();
        if(null != netBroker){
            netBroker = 1;
        }else{
            netBroker = 2;
        }
        brokerData.put("brokerId",id);
        brokerData.put("userTitle",userTitle);
        brokerData.put("netBroker",netBroker);
        int num = brokerService.editBrokerInfo(brokerData);
        if (0 != num) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail("调整经纪人职级失败");
        }
    }
}
